// MCE status for LCDSmartie
// Based on DemoC++Plugin.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>

#define DLLEXPORT __declspec(dllexport)

extern "C" int    __stdcall MCE_dll_start(); 
extern "C" char * __stdcall MCE_dll_getvals1();
extern "C" char * __stdcall MCE_dll_getvals2();
extern "C" char * __stdcall MCE_dll_get_tag_value(char *);
extern "C" char * __stdcall MCE_dll_get_tag_values(char *, char *);
extern "C" char * __stdcall MCE_dll_get_channel(char *);
extern "C" char * __stdcall MCE_dll_subtract( char *a, char *b);

char result[16];

/*********************************************************
 *         SmartieInit                                   *
 *********************************************************/
extern "C" DLLEXPORT  void __stdcall  SmartieInit()
{
	// This function will be called when the plugin is 1st loaded.
	// This function is OPTIONAL.
	MCE_dll_start();
}

/*********************************************************
 *         SmartieFini                                   *
 *********************************************************/
extern "C" DLLEXPORT  void __stdcall  SmartieFini()
{
	// This function will be called just before the plugin
	// is unloaded. This function is OPTIONAL.
}

/*********************************************************
 *         GetMinRefreshInterval                         *
 *********************************************************/
extern "C" DLLEXPORT  int __stdcall  GetMinRefreshInterval()
{
	//
	// Define the minimum interval that a screen should get fresh
	// data from our plugin.
	// The actual value used by Smartie will be the higher of this
	// value and of the "dll check interval" setting
	// on the Misc tab.  [This function is optional, Smartie will
	// assume 300ms if it is not provided.]
	// 
	return 300; // 300 ms
}


/*********************************************************
 *         Function 1                                    *
 *  Returns the first display line       			     *
 *********************************************************/
extern "C" DLLEXPORT  char * __stdcall  function1(char *param1, char *param2)
{
    return MCE_dll_getvals1();   
}


/*********************************************************
 *         Function 2                                    *
 *  Returns the second display line                      *
 *********************************************************/
extern "C" DLLEXPORT  char * __stdcall  function2(char *param1, char *param2)
{
    return MCE_dll_getvals2();  
}



/*********************************************************
 *         Function 3                                    *
 *  Returns the version number of my code                *
 *********************************************************/
extern "C" DLLEXPORT  char * __stdcall  function3(char *param1, char *param2)
{
    return "MCE status plugin v0.9e"; 
}
    


/*********************************************************
 *         Function 4                                    *
 *  Returns the value of the tag supplied as a parameter *
 *********************************************************/
extern "C" DLLEXPORT  char * __stdcall  function4(char *param1, char *param2)
{    
    return MCE_dll_get_tag_values(param1,param2);   
}
 

/*********************************************************
 *         Function 5                                    *
 *  Convert a number to a time string in format hh:mm:ss *
 *********************************************************/
extern "C" DLLEXPORT  char * __stdcall  function5(char *param1, char *param2)
{   
    int tr,tr_hours,tr_mins,tr_secs;
       
    if (sscanf(param1,"%d",&tr)!=1)                    // convert param 1 to an integer
      { strcpy(result,"Invalid Number"); }             // not a valid integer passed
    else
      {
        tr_hours=tr/3600;
        tr_mins=(tr-(tr_hours*3600))/60;
        tr_secs=tr%60;
        sprintf(result,"%02d:%02d:%02d",tr_hours,tr_mins,tr_secs);
      }    
    return result;   
}



/*********************************************************
 *         Function 6                                    *
 *  Given a channel number returns the name              *
 *********************************************************/
extern "C" DLLEXPORT  char * __stdcall  function6(char *param1, char *param2)
{   
    return MCE_dll_get_channel(param1);   
}



/*********************************************************
 *         Function 7                                    *
 *  subtract 2 tag values                                   *
 *********************************************************/
extern "C" DLLEXPORT  char * __stdcall  function7(char *param1, char *param2)
{   
    return MCE_dll_subtract(param1, param2);   
}
 

/*********************************************************
 *         Function 8  
 * Note you can have up to 20 functions in an LCDSmartie *
 * plugin dll                                            *
 *  get the value of a parameter as a time string        *
 * in format hh:mm:ss                                    *
 *********************************************************/
extern "C" DLLEXPORT  char * __stdcall  function8(char *param1, char *param2)
{   
    int tr,tr_hours,tr_mins,tr_secs;
    char trs[80];
       
    strcpy(trs,MCE_dll_get_tag_value(param1));
    if (sscanf(trs,"%d",&tr)!=1)  // convert param 1 to an integer
      { strcpy(result,"Invalid Number"); }                  // not a valid integer passed
    else
      {
        tr_hours=tr/3600;
        tr_mins=(tr-(tr_hours*3600))/60;
        tr_secs=tr%60;
        sprintf(result,"%02d:%02d:%02d",tr_hours,tr_mins,tr_secs);
      }    
    return result;   
}

